//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.yang.newcustomcuilian.listeners;

import java.util.Iterator;
import java.util.Objects;
import com.yang.newcustomcuilian.NewCustomCuiLian;
import com.yang.newcustomcuilian.language.Language;
import com.yang.newcustomcuilian.level.Level;
import com.yang.newcustomcuilian.managers.FurnaceManager;
import com.yang.newcustomcuilian.managers.QuenchingProtectRuneManager;
import com.yang.newcustomcuilian.newapi.NewAPI;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.block.Furnace;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.inventory.FurnaceBurnEvent;
import org.bukkit.event.inventory.FurnaceSmeltEvent;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.plugin.java.JavaPlugin;

/**
 * 监听器类,处理各种事件
 */
public class BListener implements Listener {
    public BListener() {
    }

    /**
     * 玩家加入服务器事件处理
     * 检查玩家装备等级并发送广播
     */
    @EventHandler(
        priority = EventPriority.HIGHEST
    )
    public void PlayerJoinEvent(PlayerJoinEvent e) {
        Player p = e.getPlayer();
        Level minLevel = NewAPI.getMinLevel(NewAPI.addAll(NewAPI.getItemInHand(p), p.getEquipment().getHelmet(), p.getEquipment().getChestplate(), p.getEquipment().getLeggings(), p.getEquipment().getBoots(), NewAPI.getItemInOffHand(p)));
        if (minLevel.levelValue != -1 && ((Level)NewCustomCuiLian.customCuilianManager.customCuilianLevelList.get(minLevel.levelValue)).canSendNotice) {
            Bukkit.broadcastMessage(Language.JOIN_SERVER_NOTICE.replace("%p", p.getName()));
        }

    }

    /**
     * 物品栏点击事件处理
     * 处理淬炼保护符的使用逻辑
     */
    @EventHandler(
        priority = EventPriority.HIGHEST
    )
    public void InventoryClickEvent(InventoryClickEvent e) {
        Player p = (Player)e.getWhoClicked();
        if (e.getRawSlot() >= 0) {
            ItemStack item = e.getCurrentItem();
            if (!e.isRightClick()) {
                return;
            }

            if (e.getInventory().getType() == InventoryType.CRAFTING || e.getInventory().getType() == InventoryType.PLAYER) {
                ItemMeta meta = item.getItemMeta();
                if (QuenchingProtectRuneManager.quenchingProtectRuneUsingMap.containsKey(p.getName())) {
                    if (!e.isRightClick()) {
                        return;
                    }

                    if (e.getInventory().getType() != InventoryType.PLAYER && e.getInventory().getType() != InventoryType.CRAFTING) {
                        p.sendMessage(Language.APPEND_TO_THE_RIGHT_KEY);
                    } else if (item != null && !item.getType().equals(Material.AIR)) {
                        ItemStack fitem = (ItemStack)QuenchingProtectRuneManager.quenchingProtectRuneFirstItemMap.get(p.getName());
                        if (!NewCustomCuiLian.customCuilianManager.ItemList.contains(item.getType())) {
                            e.setCancelled(true);
                            QuenchingProtectRuneManager.quenchingProtectRuneUsingMap.remove(p.getName());
                            QuenchingProtectRuneManager.quenchingProtectRuneFirstItemMap.remove(p.getName());
                            p.sendMessage(Language.CAN_NOT_ADD_PROMPT);
                        } else if (!p.getInventory().contains(fitem)) {
                            p.sendMessage(Language.CAN_NOT_ADD_PROMPT);
                            QuenchingProtectRuneManager.quenchingProtectRuneUsingMap.remove(p.getName());
                            QuenchingProtectRuneManager.quenchingProtectRuneFirstItemMap.remove(p.getName());
                            e.setCancelled(true);
                        } else if (NewAPI.getLevelByItem(item).levelValue >= NewAPI.getLevelByQuenchingProtectRune(fitem).levelValue && !Objects.equals(NewAPI.getLevelByQuenchingProtectRuneString(item).levelValue, NewAPI.getLevelByQuenchingProtectRune(fitem).levelValue)) {
                            Level l = NewAPI.getLevelByQuenchingProtectRune(fitem);
                            NewAPI.addCuilianQuenchingProtectRune(item, fitem);
                            p.sendMessage(Language.CAN_ADD_PROMPT.replace("%s", l.quenchingProtectRuneString));
                            e.setCancelled(true);
                            int sl = fitem.getAmount() - 1;
                            p.getInventory().remove(fitem);
                            ItemStack i = fitem.clone();
                            i.setAmount(sl);
                            p.getInventory().addItem(new ItemStack[]{i});
                            p.closeInventory();
                            QuenchingProtectRuneManager.quenchingProtectRuneUsingMap.remove(p.getName());
                            QuenchingProtectRuneManager.quenchingProtectRuneFirstItemMap.remove(p.getName());
                        }
                    } else {
                        e.setCancelled(true);
                        p.closeInventory();
                        p.sendMessage(Language.ADD_CANCEL_PROMPT);
                        QuenchingProtectRuneManager.quenchingProtectRuneUsingMap.remove(p.getName());
                        QuenchingProtectRuneManager.quenchingProtectRuneFirstItemMap.remove(p.getName());
                    }
                } else if (e.isRightClick() && NewAPI.isQuenchingProtectRuneMapItemMetaHasItemMeta(meta)) {
                    if (!p.getInventory().contains(item)) {
                        p.sendMessage(Language.APPEND_TO_THE_RIGHT_KEY);
                        e.setCancelled(true);
                        return;
                    }

                    QuenchingProtectRuneManager.quenchingProtectRuneUsingMap.put(p.getName(), meta);
                    QuenchingProtectRuneManager.quenchingProtectRuneFirstItemMap.put(p.getName(), item);
                    Level l2 = NewAPI.getLevelByQuenchingProtectRune(item);
                    Iterator var10 = Language.ADD_PROMPT_LIST.iterator();

                    while(var10.hasNext()) {
                        String str = (String)var10.next();
                        p.sendMessage(str.replace("%s", l2.quenchingProtectRuneString));
                    }

                    e.setCancelled(true);
                    p.closeInventory();
                }
            }
        }

    }

    /**
     * 玩家交互事件处理
     * 记录玩家使用熔炉的状态
     */
    @EventHandler(
        priority = EventPriority.HIGHEST
    )
    public void PlayerInteractEvent(PlayerInteractEvent e) {
        if (e.getAction().equals(Action.RIGHT_CLICK_BLOCK) && e.hasBlock() && e.getClickedBlock().getType().equals(Material.FURNACE)) {
            Player p = e.getPlayer();
            Furnace f = (Furnace)e.getClickedBlock().getState();
            if (FurnaceManager.furnaceUsingMap.get(f.getLocation()) != null) {
                FurnaceManager.furnaceUsingMap.remove(f.getLocation());
            }

            FurnaceManager.furnaceUsingMap.put(f.getLocation(), p.getName());
        }

    }

    /**
     * 熔炉燃烧事件处理
     * 处理淬炼宝石的燃烧逻辑
     */
    @EventHandler(
        priority = EventPriority.HIGHEST
    )
    public void FurnaceBurnEvent(FurnaceBurnEvent e) {
        ItemStack fuel = e.getFuel();
        Furnace f = (Furnace)e.getBlock().getState();
        if ((!NewAPI.isStoneMapItemMetaHasItemMeta(fuel.getItemMeta()) || NewCustomCuiLian.customCuilianManager.ItemList.contains(f.getInventory().getSmelting().getType())) && (!NewCustomCuiLian.customCuilianManager.ItemList.contains(f.getInventory().getSmelting().getType()) || NewAPI.isStoneMapItemMetaHasItemMeta(fuel.getItemMeta())) && NewAPI.getLevelByItem(f.getInventory().getSmelting()).levelValue + NewAPI.getStoneByItem(fuel).riseLevel <= NewCustomCuiLian.Max) {
            if (NewAPI.isStoneMapItemMetaHasItemMeta(fuel.getItemMeta())) {
                if (NewCustomCuiLian.customCuilianManager.ItemList.contains(f.getInventory().getSmelting().getType())) {
                    FurnaceManager.furnaceFuelMap.put(f.getLocation(), fuel);
                    e.setBurning(true);
                    e.setBurnTime(200);
                    return;
                }

                e.setCancelled(true);
            }
        } else {
            e.setCancelled(true);
        }

    }

    /**
     * 熔炉冶炼事件处理
     * 处理装备淬炼的结果
     */
    @EventHandler(
        priority = EventPriority.HIGHEST
    )
    public void FurnaceSmeltEvent(FurnaceSmeltEvent e) {
        ItemStack smelt = e.getSource();
        Furnace f = (Furnace)e.getBlock().getState();
        ItemStack fuel = (ItemStack)FurnaceManager.furnaceFuelMap.get(f.getLocation());
        if (!FurnaceManager.furnaceFuelMap.containsKey(f.getLocation()) && NewCustomCuiLian.customCuilianManager.ItemList.contains(smelt.getType())) {
            e.setResult(smelt);
        } else if (FurnaceManager.furnaceFuelMap.containsKey(f.getLocation()) && NewCustomCuiLian.customCuilianManager.ItemList.contains(smelt.getType()) && fuel != null && NewAPI.isStoneMapItemMetaHasItemMeta(fuel.getItemMeta()) && FurnaceManager.furnaceUsingMap.get(f.getLocation()) != null) {
            Player p = JavaPlugin.getPlugin(NewCustomCuiLian.class).getServer().getPlayer((String)FurnaceManager.furnaceUsingMap.get(f.getLocation()));
            smelt.setAmount(1);
            e.setResult(NewAPI.cuilian(fuel, smelt, p));
            FurnaceManager.furnaceFuelMap.remove(f.getLocation());
        }

    }
}
